package com.epam.manager.controller;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.apache.log4j.Logger;

import com.epam.DAO.BillDAO;
import com.epam.DAO.CarDAO;
import com.epam.entity.Bill;
import com.epam.entity.Car;
import com.epam.entity.Request;
import com.epam.entity.Status;

public class ActiveRequestManagedBean extends RequestController {

	private static Logger log = Logger
			.getLogger(ActiveRequestManagedBean.class);

	private DataModel<Bill> bills;
	private Request request;
	private Bill bill;

	private BillDAO billDAO;
	private CarDAO carDAO;
	
	private int page = 1;
	
	public ActiveRequestManagedBean() {
		super();
		bills = new ListDataModel<Bill>();
		carDAO = daoFactory.getCarDAO();
		billDAO = daoFactory.getBillDAO();
		bill = new Bill();
	}

	public Bill getBill() {
		return bill;
	}

	public void setBill(Bill bill) {
		this.bill = bill;
	}

	public Request getRequest() {
		return request;
	}

	public void setRequest(Request request) {
		this.request = request;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	@Override
	public DataModel<Request> getRequests() {
		requests.setWrappedData(requestDAO.getRequests(Status.ACTIVE));
		return requests;
	}

	public void initRequest() {
		request = requests.getRowData();
	}

	public void closeRequest() {
		log.debug("request " + request);
		request.setStatus(Status.CLOSED);
		requestDAO.update(request);

		Car car = request.getCar();
		car.setIsAvaliable(true);
		carDAO.update(car);
	}

	public void makeAdditionalBill() {	
		bill.setRequest(request);
		bill.setStatus(Status.NOT_PAYED);
		billDAO.create(bill);
	}

	public DataModel<Bill> getBills() {
		bills.setWrappedData(billDAO.getRequestBills(requests.getRowData()));
		return bills;
	}

	public void setBills(DataModel<Bill> bills) {
		this.bills = bills;
	}

}
